package cn.tedu.tmall.admin.mall.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class TimerAspect {

    @Pointcut("execution(* cn.tedu.tmall.admin.mall.service.*.*(..))")
    public void serviceMethod() {}

    @Around("serviceMethod()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 通过ProceedingJoinPoint获取连接点的相关信息
        Class<?> className = pjp.getTarget().getClass();
        String methodName = pjp.getSignature().getName();
        Object[] args = pjp.getArgs();
        System.out.println("类名：" + className);
        System.out.println("方法名：" + methodName);
        System.out.println("参数列表：" + Arrays.toString(args));

        long start = System.currentTimeMillis();
        Object result = pjp.proceed();
        long end = System.currentTimeMillis();
        System.out.println("执行耗时：" + (end - start));
        return result;
    }

    // ===== 其它通知（不需要掌握，仅掌握@Around即可） =====

    @Before("execution(* cn.tedu.tmall.admin.mall.service.*.*(..))")
    public void before() {}

    @AfterReturning(value = "execution(* cn.tedu.tmall.admin.mall.service.*.*(..))",
            returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {}

    @AfterThrowing(value = "execution(* cn.tedu.tmall.admin.mall.service.*.*(..))",
            throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Throwable e) {}

    @After("execution(* cn.tedu.tmall.admin.mall.service.*.*(..))")
    public void after() {}

}
